package org.qf.web;

import com.github.pagehelper.PageInfo;
import org.qf.pojo.*;
import org.qf.service.ProviderService;
import org.qf.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class ProvideController {

    @Resource
    private ProviderService providerService;

    // 显示出供应商
    @RequestMapping("/showProvider")
    public String providerList(Model model, @RequestParam(defaultValue = "1") int pageNum, String name) {
        //去数据库查询，把数据查询出来，再跳转页面
        System.out.println("======================");
        PageInfo<Provider> pageInfo = providerService.showProvider(pageNum,name);
        System.out.println(pageInfo + "======================");
        //把集合数据存储起来
        model.addAttribute("pageInfo", pageInfo);
        return "providerList";
    }

    // 添加供应商
    @RequestMapping("/addProvider")
    public String addProvider(HttpServletResponse response, HttpSession session, Model model, Provider provider) {
        int i= providerService.addProvider(provider);
        if (i>0)
        {
            model.addAttribute("result","添加成功!");
            return "redirect:/showProvider";
        }
        else {
            model.addAttribute("result","添加失败!");
            return "addProvider";
        }
    }
    // 添加供应商
    @RequestMapping("/addProviderHtml")
    public String addProviderHtml(HttpServletResponse response, HttpSession session, Model model, Provider provider) {
        return "providerAdd";

    }

    @RequestMapping("/EditProvider")
    public String EditProvider(HttpServletResponse response,Provider provider,Model model) {
        int i=providerService.EditProvider(provider);
        if(i>0){
            //增加成功
            return "redirect:/showProvider";
        }else{
            return "editProvider";
        }
    }

    @RequestMapping("/EditProviderHtml")
    public String EditUserHtml(HttpServletResponse response,String id,Model model,HttpSession session) {
        Provider provider=providerService.selectByID(id);
        model.addAttribute("provider",provider);
        return "providerUpdate";
    }

    @RequestMapping("/ProviderHtml")
    public String ProviderHtml(HttpServletResponse response,Provider provider,Model model) {
        return "providerList";
    }


    // 删除某个供应商
    @RequestMapping("/deleteProvider")
    public String deleteProvider(Model model,HttpSession session,Provider provider){
        int i=providerService.DeleteProvider(provider);
        if (i>0)
        {
            model.addAttribute("result","删除成功!");
        }
        else {
            model.addAttribute("result","删除失败!");
        }
        return "redirect:/showProvider";
    }
    @RequestMapping("/detailProvider")
    public String detailProvider(Model model, HttpSession session, Provider provider){
        Provider provider1=providerService.selectById(provider.getProid());

        model.addAttribute("provider",provider1);
        return "providerView";
    }
}
